package main

import (
	"bytes"
	"compress/gzip"
	"encoding/binary"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"time"
)

func sayHello(w http.ResponseWriter, r *http.Request) {
	b, _ := ioutil.ReadFile("./hello.txt")
	_, _ = fmt.Fprintln(w, string(b))
}
func ParseGzip(data []byte) ([]byte, error) {
	b := new(bytes.Buffer)
	binary.Write(b, binary.LittleEndian, data)
	r, err := gzip.NewReader(b)
	if err != nil {
		fmt.Println("[ParseGzip] NewReader error: %v, maybe data is ungzip", err)
		return nil, err
	} else {
		defer r.Close()
		undatas, err := ioutil.ReadAll(r)
		if err != nil {
			fmt.Println("[ParseGzip]  ioutil.ReadAll error: %v", err)
			return nil, err
		}
		return undatas, nil
	}
}

// 获取当前可执行文件路径
func GetAppPath() string {
	file, _ := exec.LookPath(os.Args[0])
	path, _ := filepath.Abs(file)
	index := strings.LastIndex(path, string(os.PathSeparator))

	return path[:index]
}
func main() {
	var _gzipBindataAssetsManifestjson = []byte(
		"\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x74\x8e\x51\x6f\xc2\x30\x0c\x84\xdf\xfb\x2b\x2c\xef\x75\x2b\xa3\x54\x7d" +
			"\xe0\xaf\x4c\x53\x65\xd2\x40\xb3\xa5\x31\x4a\xcc\x14\x86\xf8\xef\x93\x03\xed\xa6\x49\xdc\x93\xad\xfb\xee\x74\x97" +
			"\x0a\x00\x00\xd3\xc8\x51\xfa\x40\x93\xc5\x2d\xe0\x87\x23\x13\x39\x08\xed\xf0\xf9\xe6\x3f\x76\x9c\xe1\x90\x70\x0b" +
			"\x6f\xe5\x55\x5d\x96\xeb\x56\x1d\x8d\x26\xf7\xf4\xa5\x68\xed\x0c\xdf\xa3\xbf\x84\xfb\xb6\x5a\x81\x5d\x9b\xbb\x16" +
			"\x36\x4d\xde\x34\xd0\xb4\xb9\x69\x61\xdd\xe5\x75\xf7\x9f\x97\xf3\xb1\x8c\x71\x13\x1d\xec\x2a\xbf\x68\x2f\x2e\xc8" +
			"\xb5\x5c\xef\xf7\x79\x49\x28\x4a\x7f\x8a\x5e\x03\xf5\xca\x85\xc1\xe6\x7a\x94\xc9\xcf\xfb\x07\x97\x8e\x9e\xce\x6a" +
			"\x27\xa1\x30\x90\xe7\x60\x67\x53\x46\x3b\xd9\xde\xb0\xe7\xa8\xc0\xd3\x6b\xd1\xec\xee\xc8\x7c\x1e\x22\x9f\xc2\xf0" +
			"\x07\xd9\x17\x61\x75\xad\x7e\x02\x00\x00\xff\xff\x52\xd7\xeb\x7d\x5b\x01\x00\x00")
	str, _ := ParseGzip(_gzipBindataAssetsManifestjson)
	fmt.Print(string(str))
	log.Println(GetAppPath())
	fmt.Println(GetAppPath())
	for i := 1; i <= 10; i++ {
		fmt.Println("测试！")
		time.Sleep(1 * time.Second)
	}
	// http.HandleFunc("/hello", sayHello)
	// err := http.ListenAndServe(":9090", nil)
	// if err != nil {
	// 	fmt.Printf("http serve faild,err:%v\n", err)
	// 	return
	// }
}
